#!/usr/bin/php -q
<?#
#
# This header lets the init script be detected by redhat-config-services.
#
# putting php's <? opening before # makes chkconfig unable to parse some
# of the header data. This should be fixed in chkconfig.
#
# run 'chkconfig --add webcp' to enable it.
#
# chkconfig: - 85 15
# description: Starts and stops the webcp and webcp httpd daemons.
# processname: webcp.php
# processname: webcp-httpd.php
# pidfile: /var/run/webcp/webcp.pid
# pidfile: /var/run/webcp/webcp-httpd.pid
# config:  /home/webcp/web/config.inc.php
	/*
	// File:	webcp-init.php
	// Purpose:	Starts and stops the webcp and webcp httpd daemons.
	// Creation:	2003-06-04
	// Author:	Jonathan Haskins <jhaskins@killobyte.com>
	*/
error_reporting(E_ALL);
$install_dir = '/home/webcp';
include("$install_dir/web/config.inc.php");
$args = trim(next($_SERVER['argv']));
switch($args) {
	case 'start':
		$status = start();
		break;
	case 'stop':
		$status = stop();
		break;
	case 'restart':
		$status = restart();
		break;
	case 'reload':
		$status = reload();
		break;
	case 'status':
		$status = status();
		break;
	default:
		echo "usage: webcp (start|stop|restart|reload|status|help)\n\n";
		echo "start			- start web-cp\n";
		echo "stop			- stop web-cp\n";
		echo "restart			- restart web-cp\n";
		echo "reload			- reload the configuration file (not functional)\n";
		echo "status			- shows whether web-cp is running or not\n";
		echo "help			- display this help notice\n\n";
		$status = 0;
		break;
}
/* work around for weird status returns in php < 4.3 */ 
if ($status == 0) {
	return(0);
} else {
	exit(1);
}
function start() {
	global $cfg, $install_dir;
	$status = 0;
	echo 'Starting web-cp daemon:				';
	if (($pid = get_pid($cfg['pid'])) && is_running($pid)) {
		echo "[ OK ]\n";
	} else {
		exec("$install_dir/server/webcp.php -d > /dev/null", $webcp_output, $webcp_return);
		if ($webcp_return !== 0) {
			$status = 1;
			echo "[ FAILED ]\n";
		} else {
			echo "[ OK ]\n";
		}
	}
	if ($cfg['httpd_mode'] == 'webcp') {
		echo 'Starting web-cp http daemon:				';
		if (($pid = get_pid($cfg['httpd_pid'])) && is_running($pid)) {
			echo "[ OK ]\n";
		} else {
			exec("$install_dir/server/webcp-httpd.php -d > /dev/null", $httpd_output, $httpd_return);
			if ($httpd_return !== 0) {
				$status = 1;
				echo "[ FAILED ]\n";
			} else {
				echo "[ OK ]\n";
			}
		}
	}
	return $status;
}
function stop() {
	global $cfg;
	$status = 0;
	if ($cfg['httpd_mode'] == 'webcp') {
		echo 'Stopping web-cp http daemon:				';
		if ($pid = get_pid($cfg['httpd_pid'])) {
			if (!posix_kill($pid, 15)) {
				$status = 1;
				echo "[ FAILED ]\n";
			} else {
				echo "[ OK ]\n";
			}
		} else {
			$status = 1;
			echo "[ FAILED ]\n";
		}
	}
	echo 'Stopping web-cp daemon:				';
	if ($pid = get_pid($cfg['pid'])) {
		if (!posix_kill($pid, 15)) {
			$status = 1;
			echo "[ FAILED ]\n";
		} else {
			echo "[ OK ]\n";
		}
	} else {
		$status = 1;
		echo "[ FAILED ]\n";
	}
	return $status;
}
function restart() {
	$status1 = stop();
	sleep(1);
	$status2 = start();
	return $status1 + $status2 == 0 ? 0 : 1;
}
function reload() {
	global $cfg;
	$status = 0;
	
	if ($cfg['httpd_mode'] == 'webcp') {
		echo 'Reloading web-cp http daemon:				';
		if ($pid = get_pid($cfg['httpd_pid'])) {
			if (!posix_kill($pid, 1)) {
				$status = 1;
				echo "[ FAILED ]\n";
			} else {
				echo "[ OK ]\n";
			}
		} else {
			$status = 1;
			echo "[ FAILED ]\n";
		}
	}
	return $status;
}
function status() {
	global $cfg;
	$output = "web-cp is stopped\n";
	if (($pid = get_pid($cfg['pid'])) && is_running($pid)) $webcp_pid = $pid;
	if (($pid = get_pid($cfg['httpd_pid'])) && is_running($pid)) $httpd_pid = $pid;
	if (isset($webcp_pid) || isset($httpd_pid)) {
		$output = 'web-cp (pid';
		if (isset($webcp_pid)) $output .= " $webcp_pid";
		if (isset($httpd_pid)) $output .= " $httpd_pid";
		$output .= ") is running...\n";
	}
	echo $output;
	return 0;
}
function get_pid($pid_file) {
	if (is_readable($pid_file) && !is_dir($pid_file)) {
		return trim(file_get_contents($pid_file));
	} else {
		return false;
	}
}
function is_running($pid) {
	if ($pid > 0 && posix_kill($pid, 0)) {
		return true;
	} else {
		return false;
	}
}
?>
